package com.guo.hui.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.taglibs.standard.extra.spath.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.guo.hui.dao.ponal.UserMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.ponal.User;
import com.guo.hui.service.UserService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	private static final String BASE_PATH = "resume/user/";

	@Autowired
	private UserService userService;
	@Autowired
	private UserMapper userDao;

	@RequestMapping(value = "/user")
	public String index() {
		return BASE_PATH + "user";
	}

	@ResponseBody
	@RequestMapping(value = "/userdata")
	public String userdata() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = userDao.selectByMap(map);
		return UtilJson.toJson(list);
	}

	@RequestMapping(value = "/adduser")
	public String adduser() {
		return BASE_PATH + "add";
	}

	@RequestMapping(value = "/updateuser")
	public String updateuser(Model mm, @RequestParam Integer id) {
		User user = userDao.selectByKey(id);
		mm.addAttribute("user", user);
		return BASE_PATH + "update";
	}

	@ResponseBody
	@RequestMapping("/deleteuser")
	public String deleteuser(@RequestParam String ids) {
		List<Integer> list = UtilJson.fromJsonType(ids, new TypeToken<List<Integer>>() {
		}.getType());
		// int[] uids = UtilJson.fromJsonType(ids, new
		// TypeToken<int[]>(){}.getType());
		List<User> listuser = userDao.batchSelectList(list);
		for (User u : listuser) {
			u.setDelflag(DelFlag.LOSE.toString());
			u.setModifyDate(new Date());
		}
		userDao.batchUpdate(listuser);
		return "1";
	}

	@ResponseBody
	@RequestMapping("/usersave")
	public String usersave(@RequestParam String user) {
		User us = (User) UtilJson.fromJson(user, User.class);
		userService.saveUser(us);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/userupdate")
	public String userupdate(@RequestParam String user) {
		User us = (User) UtilJson.fromJson(user, User.class);
		userService.updateUser(us);
		return "ok";
	}

}
